<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: Render to 3D Texture</title>
    <meta charset="utf-8">
    <script src="utils/gl-matrix.js"></script>
    <script src="../build/picogl.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: Render to 3D Texture
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/render-to-3Dtexture.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/vs" id="vertex-3dtexture">
        #version 300 es

        layout(std140, column_major) uniform;

        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        layout(location=2) in vec4 normal;
        
        uniform SceneUniforms {
            mat4 viewProj;
            vec4 eyePosition;
            vec4 lightPosition;
        };

        uniform mat4 uModel;
        
        out vec3 vPosition;
        out vec2 vUV;
        out vec3 vNormal;

        void main() {
            vec4 worldPosition = uModel * position;
            vPosition = worldPosition.xyz;
            vUV = uv;
            vNormal = (uModel * normal).xyz;
            gl_Position = viewProj * worldPosition;
        }
    </script>
    <script type="x-shader/vf" id="fragment-3dtexture">
        #version 300 es
        precision highp float;

        layout(std140, column_major) uniform;

        uniform SceneUniforms {
            mat4 viewProj;
            vec4 eyePosition;
            vec4 lightPosition;
        };

        uniform sampler2D tex;
        
        in vec3 vPosition;
        in vec2 vUV;
        in vec3 vNormal;

        out vec4 fragColor;
        void main() {
            vec3 color = texture(tex, vUV).rgb;

            vec3 normal = normalize(vNormal);
            vec3 eyeVec = normalize(eyePosition.xyz - vPosition);
            vec3 incidentVec = normalize(vPosition - lightPosition.xyz);
            vec3 lightVec = -incidentVec;
            float diffuse = max(dot(lightVec, normal), 0.0);
            float highlight = pow(max(dot(eyeVec, reflect(incidentVec, normal)), 0.0), 100.0);
            float ambient = 0.1;
            fragColor = vec4(color * (diffuse + highlight + ambient), 1.0);
        }
    </script>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es

        layout(location=0) in vec4 position;
        
        uniform mat4 uMVP;
        
        out vec3 vUV;
        void main() {
            vUV = position.xyz + 0.5;
            gl_Position = uMVP * position;
            gl_PointSize = 2.0;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;
        precision lowp sampler3D;

        in vec3 vUV;

        uniform sampler3D tex;
        uniform float uTime;

        out vec4 fragColor;
        void main() {
            fragColor = texture(tex, vUV + vec3(0.0, 0.0, uTime));
            fragColor.rgb *= fragColor.a;
        }
    </script>
    <script type="text/javascript">
        utils.addTimerElement();
        
        var canvas = document.getElementById("gl-canvas");

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        var DIMENSIONS = 128;
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .clearColor(0.0, 0.0, 0.0, 1.0)
        .blendFunc(PicoGL.ONE, PicoGL.ONE_MINUS_SRC_ALPHA)
        .cullBackfaces()
        .clear();

        var timer = app.createTimer();

        // SET UP PROGRAM
        var tex3DVsSource =  document.getElementById("vertex-3dtexture").text.trim();
        var tex3DFsSource =  document.getElementById("fragment-3dtexture").text.trim();
        var tex3DProgram = app.createProgram(tex3DVsSource, tex3DFsSource);

        var vsSource =  document.getElementById("vertex-draw").text.trim();
        var fsSource =  document.getElementById("fragment-draw").text.trim();
        var program = app.createProgram(vsSource, fsSource);

        var colorTarget = app.createTexture3D(DIMENSIONS, DIMENSIONS, DIMENSIONS);

        var framebuffer = app.createFramebuffer()
        .colorTarget(0, colorTarget);

        // SET UP GEOMETRY
        var box = utils.createBox({dimensions: [1.0, 1.0, 1.0]})
        var positions = app.createVertexBuffer(PicoGL.FLOAT, 3, box.positions);
        var uv = app.createVertexBuffer(PicoGL.FLOAT, 2, box.uvs);
        var normals = app.createVertexBuffer(PicoGL.FLOAT, 3, box.normals);

        var boxArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions)
        .vertexAttributeBuffer(1, uv)
        .vertexAttributeBuffer(2, normals);

        // CREATE POINT CLOUD
        var INCREMENT = 1 / DIMENSIONS;

        var positionData = new Float32Array(DIMENSIONS * DIMENSIONS * DIMENSIONS * 3);

        var positionIndex = 0;
        var x = -0.5;
        for (var i = 0; i < DIMENSIONS; ++i) {
            var y = -0.5;
            for (var j = 0; j < DIMENSIONS; ++j) {
                var z = -0.5;
                for (var k = 0; k < DIMENSIONS; ++k) {
                    positionData[positionIndex++] = x + (Math.random() - 0.5) / (DIMENSIONS);
                    positionData[positionIndex++] = y + (Math.random() - 0.5) / (DIMENSIONS);
                    positionData[positionIndex++] = z + (Math.random() - 0.5) / (DIMENSIONS);
                    z += INCREMENT;
                }
                y += INCREMENT;
            }
            x += INCREMENT;
        }

        var pointPositions = app.createVertexBuffer(PicoGL.FLOAT, 3, positionData)

        var pointArray = app.createVertexArray()
        .vertexAttributeBuffer(0, pointPositions);

        // SET UP UNIFORM BUFFER

        var tex3DViewMatrix = mat4.create();
        var tex3DEyePosition = vec3.fromValues(0.7, 0.7, 0.7);
        mat4.lookAt(tex3DViewMatrix, tex3DEyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(0.6, 0.6, 0.6);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var tex3DProjMatrix = mat4.create();
        mat4.perspective(tex3DProjMatrix, Math.PI / 2, 1, 0.1, 10.0);

        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);
        
        var tex3DViewProjMatrix = mat4.create();
        mat4.multiply(tex3DViewProjMatrix, tex3DProjMatrix, tex3DViewMatrix);

        var mvpMatrix = mat4.create();
        mat4.multiply(mvpMatrix, projMatrix, viewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 0.5);

        var sceneUniformBuffer = app.createUniformBuffer([
            PicoGL.FLOAT_MAT4,
            PicoGL.FLOAT_VEC4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, tex3DViewProjMatrix)
        .set(1, tex3DEyePosition)
        .set(2, lightPosition)
        .update();

        var modelMatrix = mat4.create();
        var rotateXMatrix = mat4.create();
        var rotateYMatrix = mat4.create();

        var angleX = 0;
        var angleY = 0;

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);

            mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 10.0);
            mat4.multiply(mvpMatrix, projMatrix, viewMatrix);            
        }

        
        utils.loadImages(["img/webgl-logo.png"], function(images) {
            // CREATE TEXTURE
            var texture = app.createTexture2D(images[0], { flipY: true });

            // SET UP DRAW CALL
            var tex3DDrawCall = app.createDrawCall(tex3DProgram, boxArray)
            .uniformBlock("SceneUniforms", sceneUniformBuffer)
            .texture("tex", texture);

            var startTime = performance.now();

            var drawCall = app.createDrawCall(program, pointArray, PicoGL.POINTS)
            .texture("tex", colorTarget)
            .uniform("uMVP", mvpMatrix);;

            app.drawFramebuffer(framebuffer)
            .viewport(0, 0, DIMENSIONS, DIMENSIONS)
            .depthTest()
            .clearColor(0.0, 0.0, 0.0, 0.0)
            

            function draw3Dtexture() {

                for (var i = 0; i < DIMENSIONS; ++i) {
                    angleX += Math.PI / DIMENSIONS;
                    angleY += 0.5 * Math.PI / DIMENSIONS;

                    mat4.fromXRotation(rotateXMatrix, angleX);
                    mat4.fromYRotation(rotateYMatrix, angleY);
                    mat4.multiply(modelMatrix, rotateXMatrix, rotateYMatrix);


                    tex3DDrawCall.uniform("uModel", modelMatrix);

                    framebuffer.colorTarget(0, colorTarget, i);
                    
                    app.clear();
                    tex3DDrawCall.draw(); 
                }               
            
                app.defaultDrawFramebuffer()
                .defaultViewport()
                .noDepthTest()
                .blend()
                .clearColor(0.0, 0.0, 0.0, 1.0)
                requestAnimationFrame(drawMain);
            }

            function drawMain() {
                if (timer.ready()) {
                    utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
                }

                timer.start();

                drawCall.uniform("uTime", (performance.now() - startTime) / 1000);

                app.clear();
                drawCall.draw();
                
                timer.end();

                requestAnimationFrame(drawMain);
            }

            requestAnimationFrame(draw3Dtexture);
            
        });

    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>
